<template>
  <div class="loginIndex">
    <div style="height: 5vh;
    display: flex;
    justify-content: space-between;margin-left: 3vw;">
      <div  style="display: flex;
    align-items: center;">
        <img  src="../assets/logo/logo-back.png" style="height: 50px"><!---->
        <div  style=" height: 15px; display: inline-block; background-color: #dcdfe6;
    width: 1px;
    height: 1em;
    margin: 0 8px;
    vertical-align: middle;
    position: relative;"><!---->
        </div>
        <div  style="height: 25px;
                    line-height: 25px;
                    font-size: 12px;
                    font-family: PingFangSC,PingFangSC-Regular;
                    color: #333;
                    left: 230px;"
        >
          润数发票管理系统
        </div>
      </div>
      <div style="display: flex;
                  align-items: center;">
        <div style=" display: flex;align-items: center;margin-right: 3vw;">
          <img style="width: 20px; margin-right: 4px;" src="../assets/images/zh.png">
          <div style="cursor: pointer; height: 20px; line-height: 20px; color: #606266; font-size: 12px;" aria-haspopup="list" aria-controls="dropdown-menu-9884" role="button" tabindex="0">
            简体中文
          </div>
        </div>
      </div>
    </div>
    <div class="login" style="height: 91vh;">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h3 class="title" style="
    font-size: 17px;
    color: #333;
    font-weight: 600;
">欢迎登录发票管理系统</h3>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <span style="float:right;"><el-link type="primary" @click="jumpToForget()">忘记密码</el-link></span>
        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="loginFooter">
      <div class="login-footer">
        <div></div>
        <a
          class="login-footer-cell no-border"
          style="color: rgb(153, 153, 153)"
        >© Copyright 2024 上海润数有限公司版权所有</a
        >
      </div>
    </div>

  </div>
</template>

<script>
    import { getCodeImg, getNotice, getInquiry } from "@/api/login";
    import Cookies from "js-cookie";
    import { encrypt, decrypt } from '@/utils/jsencrypt'

    export default {
        name: "Login",
        data() {
            return {
                codeUrl: "",
                cookiePassword: "",
                loginForm: {
                    username: "",
                    password: "",
                    rememberMe: false,
                    code: "",
                    uuid: ""
                },
                loginRules: {
                    username: [
                        { required: true, trigger: "blur", message: "用户名不能为空" }
                    ],
                    password: [
                        { required: true, trigger: "blur", message: "密码不能为空" }
                    ],
                    code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
                },
                loading: false,
                redirect: undefined,
                // 通知列表
                noticeList:[],
                // 询价列表
                inquiryList:[],
            };
        },
        watch: {
            $route: {
                handler: function(route) {
                    this.redirect = route.query && route.query.redirect;
                },
                immediate: true
            }
        },
        created() {
            this.getCode();
            this.getCookie();
            this.getNoticeList();
            this.getInquiryList();
        },
        methods: {
            getCode() {
                getCodeImg().then(res => {
                    this.codeUrl = "data:image/gif;base64," + res.img;
                    this.loginForm.uuid = res.uuid;
                });
            },
            getNoticeList() {
                getNotice(3).then(res => {
                    this.noticeList = res.data;
                });
            },
            getInquiryList() {
                getInquiry(3).then(res => {
                    this.inquiryList = res.data;
                });
            },
            getCookie() {
                const username = Cookies.get("username");
                const password = Cookies.get("password");
                const rememberMe = Cookies.get('rememberMe')
                this.loginForm = {
                    username: username === undefined ? this.loginForm.username : username,
                    password: password === undefined ? this.loginForm.password : decrypt(password),
                    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
                };
            },
            handleLogin() {
                this.$refs.loginForm.validate(valid => {
                    if (valid) {
                        this.loading = true;
                        if (this.loginForm.rememberMe) {
                            Cookies.set("username", this.loginForm.username, { expires: 30 });
                            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
                            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
                        } else {
                            Cookies.remove("username");
                            Cookies.remove("password");
                            Cookies.remove('rememberMe');
                        }
                        this.$store.dispatch("Login", this.loginForm).then(() => {
                            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
                        }).catch(() => {
                            this.loading = false;
                            this.getCode();
                        });
                    }
                });
            },
            jumpToNotice(){
                this.$router.push('/notice');
            },
            jumpToInquiry(){
                this.$router.push('/inquiry');
            },
            jumpToForget(){
                this.$router.push('/forget');
            }
        }
    };
</script>

<style rel="stylesheet/scss" lang="scss">
  .loginIndex{

  }
  .loginIndex .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    // background-image: url("../assets/images/login-background.jpg");
    background-image: url("../assets/images/loginbottom.jpg");
    background-size: cover;
  }
  .loginIndex .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
  }

  .loginIndex .login-form {
    position: absolute;
    top: 23vh;
    right: 12vw;
    border-radius: 6px;
    background: #ffffff;
    width: 350px;
    padding: 25px 25px 5px 25px;
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }
  .loginIndex .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .loginIndex .login-code {
    width: 33%;
    height: 38px;
    float: right;
    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }
  .loginIndex .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    /*color: #fff;*/
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
    background: #f3f5f8;
  }
  .loginIndex .login-code-img {
    height: 38px;
  }

  .loginIndex .listBody{
    width: 48%;
    padding: 0 3%;
  }
  .loginIndex .listBody .list{
    display: flex;
    align-items: center;
  }

  .loginIndex .listBody .showList{
    margin-top: 2.5vh;
    font-size: 13px;
    height: 10vh;
    overflow: hidden;
  }

  .loginIndex .listBody .showList .list{
    margin-bottom: 1.5vh;
    cursor: pointer;
  }

  .loginIndex .listBody .list .titleDesc{
    width: 82%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .loginIndex .listBody .list .titleFlex, .commonList .listBody .list{
    display: flex;
    align-items: center;
  }
  .loginIndex .listBody .list .showMore{
    width: 120px;
    text-align: right;
    white-space: nowrap;
    color: #666;
    font-size: 12px
  }

  .loginIndex .loginFooter {
    width: 100%;
    height: 30px;
    background: #f3f5f8;
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .loginIndex .login-footer{
    width: 80%;
    position: absolute;
    left: 10vw;
    bottom: 5px;
    display: flex;
    align-items: flex-end;
    justify-content: center;
  }

  .loginIndex .login-runshu-logo{
    width: 30px;
    margin-right: 10px;
  }
  .loginIndex .login-footer-cell {
    font-size: 12px;
    cursor: pointer;
    color: #666;
    padding: 0 5px;
    height: 12px;
    display: inline-flex;
    align-items: center;
    border-right: 1px solid #666;
    height: 16px;
  }
  .loginIndex .login-footer-cell.no-border {
    border-right: none;
  }


</style>
